package cc.rboot.inject.configuration;

import cc.rboot.inject.aspect.InjectSetterAspect;
import cc.rboot.inject.interceptor.InjectInterceptor;
import cc.rboot.inject.register.InjectMapperRegistry;
import cc.rboot.inject.resolver.InjectBodyResolver;
import cc.rboot.inject.resolver.InjectTokenResolver;
import cc.rboot.token.core.ITokenAware;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Configuration
@ConditionalOnBean(ITokenAware.class)
@Import({InjectSetterAspect.class, InjectInterceptor.class, InjectMapperRegistry.class, InjectBodyResolver.class, InjectTokenResolver.class})
public class RBootInjectConfiguration implements WebMvcConfigurer {

    @Resource
    public InjectTokenResolver injectTokenResolver;

    @Resource
    public InjectInterceptor injectInterceptor;

    @Resource
    public InjectBodyResolver injectBodyResolver;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(injectInterceptor)
                .addPathPatterns("/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(injectBodyResolver);
        resolvers.add(injectTokenResolver);
    }
}
